﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Database;
using QLK.Database.BLLs;
using System.IO;
using QLK.Web.App_Code;

namespace QLK.Web
{
    public partial class Products : System.Web.UI.Page
    {
        private string _ProID;
        ProductBLL ProBLL = new ProductBLL();
        CategoryBLL CateBLL = new CategoryBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Load_Data();
            }
        }

        private void Load_Data()
        {
            _ProID = Request.QueryString[Columns.ProductID];

            // Load cboCateID
            drlCateID.DataSource = CateBLL.Cate_GetAll();
            drlCateID.DataTextField = Columns.CategoryName;
            drlCateID.DataValueField = Columns.CategoryID;
            drlCateID.DataBind();

            if (string.IsNullOrEmpty(_ProID))
            {
                txtProName.Text = null;
                txtUPC.Text = null;
                txtUPC.Focus();
            }
            else
            {
                Product products = new Product();
                GetProductFromProductDetails(products);

                txtUPC.Text = products.UPC;
                txtProName.Text = products.ProductName;
                drlCateID.SelectedIndex = products.CategoryID;
                drlManufacture.SelectedItem.Text = products.Manufacture;
                ImgProduct.ImageUrl = products.Image;

            }
        }

        private void GetProductFromProductDetails(Product products)
        {
            products.ProductID = int.Parse(Request.QueryString[Columns.ProductID].ToString());
            products.UPC = Request.QueryString[Columns.UPC].ToString();
            products.ProductName = Request.QueryString[Columns.ProductName].ToString();
            products.CategoryID = int.Parse(Request.QueryString[Columns.CategoryID].ToString());
            products.Manufacture = Request.QueryString[Columns.Manufacture].ToString();
            products.Image = Request.QueryString[Columns.Image].ToString();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                // Call a helper method routine to save the file.
                SaveFile(FileUpload1.PostedFile);
                ImgProduct.ImageUrl = "~/upload/" + FileUpload1.FileName.ToString();
            }
            else
                // Notify the user that a file was not uploaded.
                lblWarning.Text = "You did not specify a file to upload.";
        }

        private void SaveFile(HttpPostedFile httpPostedFile)
        {
            // Specify the path to save the uploaded file to.
            string savePath = HttpContext.Current.Server.MapPath("~/upload/");

            // Get the name of the file to upload.
            string fileName = FileUpload1.FileName;

            // Create the path and file name to check for duplicates.
            string pathToCheck = savePath + fileName;

            // Create a temporary file name to use for checking duplicates.
            string tempfileName = "";

            // Check to see if a file already exists with the
            // same name as the file to upload.        
            if (File.Exists(pathToCheck))
            {
                int counter = 2;
                while (File.Exists(pathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempfileName = counter.ToString() + fileName;
                    pathToCheck = savePath + tempfileName;
                    counter++;
                }

                fileName = tempfileName;

                // Notify the user that the file name was changed. 
                lblWarning.Text = "A file with the same name already exists, your file was saved as " + fileName;
            }
            else
            {
                // Notify the user that the file was saved successfully.
                lblWarning.Text = "Your file was uploaded successfully.";
            }

            // Append the name of the file to upload to the path.
            savePath += fileName;

            // Call the SaveAs method to save the uploaded
            // file to the specified directory.
            FileUpload1.SaveAs(savePath);
        }

        protected void btnProducts_Click(object sender, EventArgs e)
        {
            Response.Redirect(PageUrls.ProductsDetails);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_ProID))
            {
                if (drlManufacture.SelectedIndex == 0)
                    lblWarning.Text = "You did not choose the manufacture of product " + txtProName.Text.ToString();
                else
                {
                    Product products = new Product();

                    products.UPC = txtUPC.Text.ToString();
                    products.ProductName = txtProName.Text.ToString();
                    products.CategoryID = int.Parse(drlCateID.SelectedValue.ToString());
                    products.Manufacture = drlManufacture.SelectedValue.ToString();
                    products.Image = ImgProduct.ImageUrl.ToString();

                    if (ProBLL.ProductInsert(products))
                    {
                        Load_Data();
                        lblWarning.Text = "Insert product: " + txtUPC.Text.ToString() + " successfully!";
                    }
                    else
                    {
                        lblWarning.Text = "* This UPC (Universal Product Code) already existed!)";
                    }
                }
            }
            else
            {
                Product products = new Product();

                products.ProductID = int.Parse(_ProID);
                products.ProductName = txtProName.Text.ToString();
                products.UPC = txtUPC.Text.ToString();
                products.CategoryID = int.Parse(drlCateID.SelectedValue.ToString());
                products.Manufacture = drlManufacture.SelectedItem.ToString();
                products.Image = ImgProduct.ImageUrl;

                if (ProBLL.ProductUpdate(products))
                {
                    Load_Data();
                    lblWarning.Text = "Update successfully!";
                }
                else
                    lblWarning.Text = "Update failed!";
            }
        }
    }
}
